package com.ourpackage.tunestone.parsers;

import java.awt.Image;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import javax.swing.ImageIcon;

import net.htmlparser.jericho.*;

import org.apache.log4j.Logger;

import com.ourpackage.tunestone.entities.Artist;
import com.ourpackage.tunestone.entities.Info;
import com.ourpackage.tunestone.entities.Release;
import com.ourpackage.tunestone.entities.SiteSource;


public class RollingstoneSiteParser implements ISiteParser {	
	private static final Logger log = Logger.getLogger(RollingstoneSiteParser.class);
	private List<Release> releases = new ArrayList<Release>();
	private DateFormat df = new SimpleDateFormat("M d, yyyy");

	private SiteSource siteSource;

	public RollingstoneSiteParser(SiteSource siteSource) {
		this.siteSource = siteSource;
	}

	@Override
	public String getSource() {
		return siteSource.getTitle();
	}

	@Override
	public boolean isValid() {

		return true;
	}

	@Override
	public List<?> getResults() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void doParse() {
		MicrosoftConditionalCommentTagTypes.register();
		MasonTagTypes.register();
		Source source = null;
		try {
			source = new Source(siteSource.getUrl());
		} catch (IOException e1) {
			e1.printStackTrace();
		}

		Element cont = source.getElementById("storyTextContainer");

		for (StartTag e : cont.getAllStartTagsByClass("reviewSection-Contents")) {
			String reviewLink = "http://www.rollingstone.com" + e.getNextTag().getNextTag().getElement().getAttributeValue("href");
			System.out.println("Getting " + reviewLink);
			Release release = parseReview(reviewLink);
			releases.add(release);
			System.out.println("--------------------------------------");
		}		

	}

	private Release parseReview(String reviewLink) {
		Release release = new Release();
		try {
			Source source=new Source(new URL(reviewLink));
			Element cont = source.getElementById("col1_content");

			String picLink = cont.getFirstStartTag("img").getAttributeValue("src");
			System.out.println("Picture: " +  picLink);
			release.setImage(getImage(picLink));

			Element el = cont.getFirstStartTagByClass("reviewSection-Contents").getElement();		
			Element artist = el.getFirstElement("h3");
			System.out.println("Artist: " + getElementText(artist));
			release.setArtist(new Artist( getElementText(artist) ));

			Element album = el.getFirstElement("h4");
			System.out.println("Album: " + getElementText(album));
			release.setTitle(getElementText(album));


			Element rating = el.getFirstElementByClass("rsReviewRow");
			System.out.println("Rating: " + parseRating(rating));
			release.addScore(siteSource, new Info(parseRating(rating)) );

			Element author = cont.getFirstElementByClass("author");
			System.out.println("Author: " + getElementText(author));
			release.setAuthor(getElementText(author));

			Element date = cont.getFirstElementByClass("date");
			System.out.println("Date: " + getElementText(date));	
//			try {
//				Date today = df.parse(getElementText(date));           
//				release.setDate(today);
//			} catch (ParseException e) {
//				e.printStackTrace();
//			}

			Element storyTextContainer = cont.getAllElements("id", "storyTextContainer", false).get(1);
			String review = "";
			for (Element e : storyTextContainer.getAllElements("p")) {
				String content = getElementText(e);
				if (content.length() > 200) {
					System.out.println(content);
					review += content;
				}
			}
			release.addScore(siteSource, new Info(review));

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return release;
	}

	private Image getImage(String picLink) {
		ImageIcon img = null;
		try {
			img = new ImageIcon(new URL(picLink));
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} finally {			
			return img.getImage();
		}
	}

	private double parseRating(Element rating) {
		String ratingString = rating.getFirstElement("span").getAttributeValue("class").split(" ")[1];
		double ratingInt = 0;
		if (ratingString.equals("halfStar")) ratingInt = 0.5;
		else if (ratingString.equals("oneStar")) ratingInt = 1;
		else if (ratingString.equals("oneHalfStar")) ratingInt = 1.5;
		else if (ratingString.equals("twoStar")) ratingInt = 2;
		else if (ratingString.equals("twoHalfStar")) ratingInt = 2.5;
		else if (ratingString.equals("threeStar")) ratingInt = 3;
		else if (ratingString.equals("threeHalfStar")) ratingInt = 3.5;
		else if (ratingString.equals("fourStar")) ratingInt = 4;
		else if (ratingString.equals("fourHalfStar")) ratingInt = 4.5;
		else if (ratingString.equals("fiveStar")) ratingInt = 5;

		return ratingInt;
	}


	private String getElementText(Element e) {
		return e.getContent().getTextExtractor().toString();
	}

	private void getTestPage() {

	}

}
